package pharao.client.views.game.navigation;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.client.view.AbstractView;
import hmvc4gwt.client.view.IView;
import hmvc4gwt.framework.client.events.AddViewEvent;
import hmvc4gwt.framework.client.events.RemoveViewEvent;

public class GameNavigationView extends AbstractView {
	
	private Panel gameContainerPanel;
	
	public GameNavigationView(Panel gameContainerPanel) {
		setGameContainerPanel(gameContainerPanel);
	}

	public void init() {
		getController().register(AddViewEvent.class, this);
		getController().register(RemoveViewEvent.class, this);
	}

	public void terminate() {		
	}

	public void handleEvent(IHmvcEvent event) {
		if (event instanceof AddViewEvent) {
			AddViewEvent addViewEvent = (AddViewEvent) event;
			addView(addViewEvent.getView());
		} else if (event instanceof RemoveViewEvent) {
			RemoveViewEvent removeViewEvent = (RemoveViewEvent) event;
			removeView(removeViewEvent.getView());
		}
	}
	
	private void addView(IView view) {
		gameContainerPanel.add(view.getWidget());
		view.setVisible(true);
	}
	
	private void removeView(IView view) {
		
		if (view == null) {
			return;
		}
		
		if (view.getWidget() == null) {
			return;
		}
		
		if (gameContainerPanel.remove(view.getWidget())) {
			view.setVisible(false);
		}
	}

	public Widget getWidget() {
		return gameContainerPanel;
	}

	private void setGameContainerPanel(Panel gameContainerPanel) {
		this.gameContainerPanel = gameContainerPanel;
	}

}
